<?php

class Model_user extends CI_Model {
    
    function __construct() {
        parent::__construct();
        
    }
/**
 * ============================GET ONE USER=============================================
*/
    public function getUser($username, $password) {
        $collection = $this->mongo_db->db->selectCollection('user');
        $user = $collection->find(array('username'=> $username, 'password'=>$password));      
        return $user;
    }

    public function checkExists($username) {
        $collection = $this->mongo_db->db->selectCollection('user');
        $bool = $collection->findOne(array('username'=>$username));
        return $bool;
    }
/**
 * ============================GET ALL USER=============================================
*/
    public function getAllUser(){
        $collection = $this->mongo_db->db->selectCollection('user');
        $users = $collection->find();      
        return $users; 
    }
/**
 * ============================GET PROFILE USER=============================================
*/
    public function getProfile($username, $password=null){
        $collection = $this->mongo_db->db->selectCollection('user');
        $profile = $collection->find(array('username'=>$username));
        return $profile;
    }

/**
 * ============================UPDATE USER=============================================
*/
    public function update_info($id_user, $username, $password, $fullname, $dob, $gender, $address, $email, $phone){
        $collection = $this->mongo_db->db->selectCollection('user');
        $userId = new MongoID($id_user);
        $lastModify = new MongoDate();
        $data = array(
            '_id' => $userId,
            'username' => $username,
            'password' => $password,
            'create_at' => $lastModify,
            'fullname' => $fullname,
            'dob' => $dob, 
            'gender' => $gender,
            'address' => $address,
            'email' => $email,
            'phone' => $phone
            );
        // $collection->update(array('username' => $username), $data);
        $collection->update(array('username' => $username), $data);
        $error = $this->mongo_db->db->lastError();
        return $error; 
    }
/**
 * ============================DELETE USER =============================================
*/
    public function delete_user($id_user){
        $collection = $this->mongo_db->db->selectCollection('user');
        $collection->remove(array('_id' => new MongoId($id_user)));
    }

//===============================INSERT NEW USER=============================================
    public function insert_user($username, $password, $fullname, $dob, $gender, $address, $email, $phone)
    {
        $collection = $this->mongo_db->db->selectCollection('user');
        $id_user = new MongoId();
        $create_at = new MongoDate();
        $data = array(
            '_id' =>$id_user,
            'username' => $username,
            'password' => $password,
            'create_at' => $create_at,
            'fullname' => $fullname,
            'dob' => $dob,
            'gender' => $gender,
            'address' => $address,
            'email' => $email,
            'phone' => $phone
            );
        $collection->insert($data);
    }

    public function insertUser($user) {
        $email = $user['email'];
        $username = $user['userName'];
        $pass = $user['password'];
        $data = array(
            'username' => $username,
            'password' => $pass,
            'email' =>$email
        );
        $collection = $this->mongo_db->db->selectCollection('user');
        try {
            $result = $collection->insert($data);
            return 'success';
        }
        catch(MongoCursorException $mce) {
            return 'fail';
        }
    }

    public function getBookmark($user) {
        $collection = $this->mongo_db->db->selectCollection('user');
        $bm = $collection->findOne(array('username'=>$user), array('bookmark'=>1));
        return $bm;
    }
    public function addBookmark($user, $arr) {
        $collection = $this->mongo_db->db->selectCollection('user');
        $bm = $collection->update(array('username'=>$user), array('$set'=>array('bookmark'=>$arr)));
        return $bm;
    }
}
?>